<?php
namespace app\admin\controller;

use app\admin\model\Admin;

class Login extends \think\Controller
{
    public function login()
    {
        if (session('?admin_info')) {
            return $this->redirect('/admin/index/index');
        }
        return view('login');
    }

    public function login_post()
    {   
        $data['username'] = trim(input('username'));
        $admin_info = Admin::where($data)->find();
        if (!$admin_info) {
            return error('该账号不存在');
        }
        $data['password'] = md5(trim(input('password')));
        $admin_info = Admin::where($data)->find();
        // dump(json_decode( json_encode( $admin_info),true));exit;
        if (!$admin_info) {
            return error('账号或密码错误');
        }
        $admin_info->update_time = time();
        $admin_info->save();

        session('admin_info', $admin_info);
        
        return success(1, '登录成功，正在跳转...');
    }

    public function logout()
    {
        session('admin_info', null);

        return redirect('Login/login');
    }

}
